<?php

namespace App\Jobs;

use App\Constant\JpushCode;
use App\Events\JpushNoticeEvent;
use App\Facades\SamLogFacade;
use App\Models\LoanApply;
use App\Models\UserGlobal;
use App\Models\UserLoanRequest;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class LoanRequestSuccessRestTwelveJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $loan;

    /**
     * LoanRequestSuccessRestTwelveJob constructor.
     * @param UserLoanRequest $loan
     */
    public function __construct(UserLoanRequest $loan)
    {
        $this->loan = $loan;
    }

    /**
     * Description:
     * Author: Sam
     * DateTime: 2019-03-26 15:43
     * @throws \MathPHP\Exception\BadParameterException
     */
    public function handle()
    {
        $loan = $this->loan;
        $loan_apply = LoanApply::where('loan_request_id',$loan->id)->first();
        SamLogFacade::debug("授信【{$loan->id}】触发剩余12小时JPUSH");
        if(!$loan_apply){
            $jpush_id = UserGlobal::where('uid',$loan->user_id)->value('jpush_registration_id');
            if($jpush_id){
                $push_data = [
                    'body'=>"您的借款申请即将超时取消，请尽快进行要款操作。",
                    'title'=>"要款提醒（剩余12小时）",
                    'audience'=>JpushCode::AUDIENCE_TYPE_TWO,
                    'to'=>[
                        $jpush_id,
                    ],
                    'extras'=>[
                        'jump_type'=>1,
                    ],
                ];
                event(new JpushNoticeEvent($push_data));
            }
        }
    }
}
